package com.toidep.entities;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.toidep.api.ParseJSON;
import com.toidep.config.Constants;

public class FreeEntity {
	public final int FREE_TYPE_TITLE_ALBUM = 1;
	public final int FREE_TYPE_TITLE_VIDEO = 2;
	public final int FREE_TYPE_ALBUM = 3;
	public final int FREE_TYPE_VIDEO = 4;
	
	private VideoEntity videoEntity = null;
	private AlbumEntity albumEntity = null;
	private int freeType = 0;
	private ArrayList<FreeEntity> listFree = null;
	
	public void setInfo(JSONObject jsonObject) {
		if (jsonObject != null) {
			// albums
			try {
				JSONArray albums = ParseJSON.getJsonArray(jsonObject, Constants.PARAM_RESPONSE_ALBUMS);
				if (albums != null && albums.length() > 0) {
					if (listFree == null)
						listFree = new ArrayList<FreeEntity>();
					FreeEntity itemTitle = new FreeEntity();
					itemTitle.freeType = FREE_TYPE_TITLE_ALBUM;
					listFree.add(itemTitle);
					
					for (int i = 0; i < albums.length(); i++) {
						FreeEntity item = new FreeEntity();
						item.freeType = FREE_TYPE_ALBUM;
						item.albumEntity = new AlbumEntity();
						item.albumEntity.setInfo((JSONObject) albums.get(i));
						listFree.add(item);
					}
				}
			} catch (JSONException e) { e.printStackTrace(); }
			
			// videos
			try {
				JSONArray videos = ParseJSON.getJsonArray(jsonObject, Constants.PARAM_RESPONSE_VIDEOS);
				if (videos != null && videos.length() > 0) {
					if (listFree == null)
						listFree = new ArrayList<FreeEntity>();
					FreeEntity itemTitle = new FreeEntity();
					itemTitle.freeType = FREE_TYPE_TITLE_VIDEO;
					listFree.add(itemTitle);
					
					for (int i = 0; i < videos.length(); i++) {
						FreeEntity item = new FreeEntity();
						item.freeType = FREE_TYPE_VIDEO;
						item.videoEntity = new VideoEntity();
						item.videoEntity.setInfo((JSONObject) videos.get(i));
						listFree.add(item);
					}
				}
			} catch (JSONException e) { e.printStackTrace(); }
		}
	}

	/**
	 * @return the videoEntity
	 */
	public VideoEntity getVideoEntity() {
		return videoEntity;
	}

	/**
	 * @param videoEntity the videoEntity to set
	 */
	public void setVideoEntity(VideoEntity videoEntity) {
		this.videoEntity = videoEntity;
	}

	/**
	 * @return the albumEntity
	 */
	public AlbumEntity getAlbumEntity() {
		return albumEntity;
	}

	/**
	 * @param albumEntity the albumEntity to set
	 */
	public void setAlbumEntity(AlbumEntity albumEntity) {
		this.albumEntity = albumEntity;
	}

	/**
	 * @return the freeType
	 */
	public int getFreeType() {
		return freeType;
	}

	/**
	 * @param freeType the freeType to set
	 */
	public void setFreeType(int freeType) {
		this.freeType = freeType;
	}

	/**
	 * @return the listFree
	 */
	public ArrayList<FreeEntity> getListFree() {
		return listFree;
	}

	/**
	 * @param listFree the listFree to set
	 */
	public void setListFree(ArrayList<FreeEntity> listFree) {
		this.listFree = listFree;
	}
}
